package com.example;

import android.app.Activity;
import android.media.MediaExtractor;
import android.media.MediaMetadataRetriever;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.*;

import java.io.IOException;

public class VideoActivity extends Activity {
    Button mPlay;
    Button mPrepare;
    Button mGrab;
    SurfaceView mVideoDisplay;
    MediaPlayer mPlayer;
    ProgressBar mProgress;
    ImageView mImage;

    private Uri mVideoUri;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mGrab = (Button)findViewById(R.id.grab);
        mPlay = (Button)findViewById(R.id.play);
        mPrepare = (Button)findViewById(R.id.prepare);
        mVideoDisplay = (SurfaceView)findViewById(R.id.video);
        mProgress = (ProgressBar)findViewById(R.id.progress);
        mPlayer = new MediaPlayer();
        mImage = (ImageView)findViewById(R.id.image);

        mPlay.setEnabled(false);
        mPrepare.setEnabled(false);

        mProgress.setMax(100);

        mPrepare.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                prepare();
            }
        });

        mPlay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                play();
            }
        });

        mGrab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                grab();
            }
        });

        mVideoDisplay.getHolder().addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder surfaceHolder) {
                mPlayer.setSurface(surfaceHolder.getSurface());
                mPrepare.setEnabled(true);
            }

            @Override
            public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
                //To change body of implemented methods use File | Settings | File Templates.
            }

            @Override
            public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
                //To change body of implemented methods use File | Settings | File Templates.
            }
        });

        mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                mPlay.setEnabled(true);
            }
        });

        mPlayer.setOnBufferingUpdateListener(new MediaPlayer.OnBufferingUpdateListener() {
            @Override
            public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
                mProgress.setProgress(i);
            }
        });
    }

    private void grab() {
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        retriever.setDataSource(this, mVideoUri);
        mImage.setImageBitmap(retriever.getFrameAtTime());
    }

    @Override
    protected void onPause() {
        mPlayer.pause();
    }

    private void play() {
        mPlayer.start();
    }

    private void prepare() {
        mVideoUri = Uri.parse("http://player.ooyala.com/player/iphone/90NXBrNTrSTFv3gBFxtc-2YxYPJlOJav.m3u8");
        try {
            mPlayer.setDataSource(this, mVideoUri);
            mPlayer.prepareAsync();
            mPrepare.setEnabled(false);
        } catch (IOException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
    }
}
